/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mark.geoidz.modules.geoidz_Core;

import com.mark.geoidz.modules.geoidz_Core.shapes.GeoidzEllipsoid;
import com.mark.geoidz.modules.geoidz_Core.shapes.GeoidzShapeFactory;
import gov.nasa.worldwind.Model;
import gov.nasa.worldwind.WorldWind;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.awt.WorldWindowGLCanvas;
import gov.nasa.worldwind.geom.LatLon;
import gov.nasa.worldwind.geom.Position;
import gov.nasa.worldwind.layers.RenderableLayer;
import gov.nasa.worldwind.render.Material;
import java.awt.BorderLayout;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//com.mark.geoidz.modules.geoidz_Core//MainView//EN",
                     autostore = false)
@TopComponent.Description(preferredID = "MainViewTopComponent",
                          //iconBase="SET/PATH/TO/ICON/HERE", 
                          persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "com.mark.geoidz.modules.geoidz_Core.MainViewTopComponent")
@ActionReference(path = "Menu/Window" /*, position = 333 */)
@TopComponent.OpenActionRegistration(displayName = "#CTL_MainViewAction",
                                     preferredID = "MainViewTopComponent")
public final class MainViewTopComponent extends TopComponent
{
   // <editor-fold defaultstate="expanded" desc="Private Members">
   
   WorldWindowGLCanvas mWorldWindCanvas = null;
   
   // </editor-fold>
   
   public MainViewTopComponent()
   {
      initComponents();
      setName(NbBundle.getMessage(MainViewTopComponent.class, "CTL_MainViewTopComponent"));
      setToolTipText(NbBundle.getMessage(MainViewTopComponent.class, "HINT_MainViewTopComponent"));
      putClientProperty(TopComponent.PROP_DRAGGING_DISABLED, Boolean.TRUE);
      putClientProperty(TopComponent.PROP_UNDOCKING_DISABLED, Boolean.TRUE);
      
      setLayout(new BorderLayout());
      
      initWorldWind();
      
      ////////////////// TEST //////////////////////
      
      RenderableLayer testLayer = new RenderableLayer();
      mWorldWindCanvas.getModel().getLayers().add(testLayer);      
      
      Position startPoint = new Position(LatLon.fromDegrees(43.047, -76.144), 50.0);
      Position center = startPoint;
      double northSouth = 2000.0;
      double vertical = 2000.0;
      double eastWest = 2000.0;
       
      GeoidzEllipsoid sphere;
       
      for (int i = 0; i < 1000; i++) 
      {
         center = new Position(  LatLon.fromDegrees(  startPoint.latitude.degrees  + ((Math.random() * 2.0) - 1.0), 
                                                      startPoint.longitude.degrees + ((Math.random() * 2.0) - 1.0)),
                                 startPoint.elevation + (Math.random() * 200.0));
         
         sphere = (GeoidzEllipsoid)(GeoidzShapeFactory.createShape(  GeoidzShapeFactory.ShapeType_e.ELLIPSOID, 
                                                                     center, northSouth, vertical, eastWest));      
         sphere.getAttributes().setInteriorMaterial(Material.YELLOW);
         sphere.getAttributes().setInteriorOpacity(0.7);
         sphere.getAttributes().setOutlineMaterial(Material.RED);
         sphere.getAttributes().setOutlineWidth(2d);
         sphere.getAttributes().setDrawInterior(true);
         sphere.getAttributes().setDrawOutline(false);
         sphere.getWorldWindShape().setAltitudeMode(WorldWind.RELATIVE_TO_GROUND);      
         testLayer.addRenderable(sphere.getWorldWindShape());
      }
      
      
      
      //////////////////////////////////////////////
   }

   /**
    * Initializes the WorldWind canvas and adds it to the center of this component.
    * @throws IllegalArgumentException
    * @throws IllegalStateException 
    */
   private void initWorldWind() throws IllegalArgumentException, IllegalStateException
   {
      mWorldWindCanvas = new WorldWindowGLCanvas();
      mWorldWindCanvas.setPreferredSize(this.getPreferredSize());
      mWorldWindCanvas.setModel((Model)(WorldWind.createConfigurationComponent(AVKey.MODEL_CLASS_NAME)));
      
      this.add(mWorldWindCanvas, BorderLayout.CENTER);
   }

   /** This method is called from within the constructor to
    * initialize the form.
    * WARNING: Do NOT modify this code. The content of this method is
    * always regenerated by the Form Editor.
    */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
   @Override
   public void componentOpened()
   {
      // TODO add custom code on component opening
   }

   @Override
   public void componentClosed()
   {
      // TODO add custom code on component closing
   }

   void writeProperties(java.util.Properties p)
   {
      // better to version settings since initial version as advocated at
      // http://wiki.apidesign.org/wiki/PropertyFiles
      p.setProperty("version", "1.0");
      // TODO store your settings
   }

   void readProperties(java.util.Properties p)
   {
      String version = p.getProperty("version");
      // TODO read your settings according to their version
   }
}
